﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace Silvermoon.Controls.Classes
{
    public class TabPageCollection : Collection<TabPage>
    {
        private TabControl tabControl;

        public TabPageCollection(TabControl tab)
            : base()
        {
            this.tabControl = tab;
        }

        protected override void ClearItems()
        {
            foreach (var tab in this)
            {
                tab.Previous = null;
                tab.Next = null;
                tab.Dispose();
            }
            base.ClearItems();
            OnCollectionChanged();
        }

        protected override void InsertItem(int index, TabPage item)
        {
            base.InsertItem(index, item);
            OnCollectionChanged();
        }

        protected override void RemoveItem(int index)
        {
            TabPage page = this[index];
            base.RemoveItem(index);
            if (page.Previous != null) page.Previous.Next = page.Next;
            if (page.Next != null) page.Next.Previous = page.Previous;
            page.Previous = null;
            page.Next = null;

            OnCollectionChanged();
            page.Dispose();

        }

        protected override void SetItem(int index, TabPage item)
        {
            base.SetItem(index, item);
            OnCollectionChanged();
        }

        private void OnCollectionChanged()
        {
            tabControl.OnTabCollectionChanged();
        }
    }
}
